﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using VisionApp.Entity;
using VisionApp.Interface;
using VisionAppManager.Model;
using System.Windows.Media.Imaging;
using CommonServiceLocator;
using OpenCvSharp;
using System.IO;
using System.Windows.Forms;

namespace VisionAppManager.ViewModel
{
    public class VMImgViewerCam1 : ViewModelBase
    {
        public Image Image { get; set; }
        private Worker _worker;
        public Worker Worker
        {
            get
            {
                return _worker;
            }
            set
            {
                _worker = value;
                RaisePropertyChanged("Worker");
            }
        }
        public VMImgViewerCam1(IWorker worker)
        {
            Worker = (Worker)worker;
            Messenger.Default.Register<HImageContainer>(this, (rc) => updateView1(rc));
            Messenger.Default.Register<VSResultContainer>(this, (rc) => updateView(rc));
        }
        private void updateView(VSResultContainer rc)
        {
            if(Image!=null && rc.ID==ImagePosID.LEFTCAM)
            {
                Image.Dispatcher.Invoke(
                new Action(
                delegate
                {
                    Image.Source = (BitmapImage)rc.Image;
                    //Image.Source.Freeze();
                }
                )
                );
            }
        }
        private void updateView1(HImageContainer rc)
        {
            Mat image = new Mat(rc.TextHeight, rc.TextWidth, MatType.CV_8UC1, rc.ImGray);
            if (Image != null && rc.ID == ImagePosID.LEFTCAM)
            {
                Image.Dispatcher.Invoke(
                new Action(
                delegate
                {
                    Image.Source = matToBitmapImage(image);
                }
                )
                );
            }
        }
        private BitmapImage matToBitmapImage(Mat image)
        {
            System.Drawing.Bitmap bitmap = matToBitmap(image);
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); // 坑点：格式选Bmp时，不带透明度
                stream.Position = 0;
                BitmapImage result = new BitmapImage();
                result.BeginInit();
                // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
                // Force the bitmap to load right now so we can dispose the stream.
                result.CacheOption = BitmapCacheOption.OnLoad;
                result.StreamSource = stream;
                result.EndInit();
                result.Freeze();
                return result;
            }
        }
        private System.Drawing.Bitmap matToBitmap(Mat image)
        {
            return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
        }
    }
}
